package controller;

import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    private void findOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取学生id （① 接收参数+封装）
        String id = req.getParameter("id");

        //调用service完成业务处理 (② 调用业务层执行业务)
        User user = userService.findOne(Integer.parseInt(id));

        //响应页面（重定向 or 转发）-- 通过域对象，将业务处理完后的数据带到页面上展示 （③ 响应页面 - 域对象带数据）
        req.setAttribute("user",user);
        req.getRequestDispatcher("/user.jsp").forward(req,resp);
    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<User> users = userService.findAll();

        req.setAttribute("users",users);
        req.getRequestDispatcher("/user.jsp").forward(req,resp);
//        req.getRequestDispatcher("/jstl1.jsp").forward(req,resp);
    }

    private void genderCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        List<Map<String, Object>> genderCount = userService.genderCount();

        req.setAttribute("genderCount",genderCount);
        req.getRequestDispatcher("/user.jsp").forward(req,resp);
    }

    private void age(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setAttribute("age",15);

        req.getRequestDispatcher("/jstl1.jsp").forward(req,resp);
    }
}
